package com.order.ctl;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * dongzhihua
 * 2022/10/21 10:18
 */
@RestController
@RequestMapping("service/discovery")
public class ServiceDiscoveryCtl {

    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("all")
    public Object all() {
        final List<String> applications = discoveryClient.getServices();
        return applications.stream()
                .map(item -> new App(item,
                        discoveryClient.getInstances(item).stream().map(c -> c.getUri().toString()).collect(Collectors.toList())))
                .collect(Collectors.toList());
    }

    @Data
    @AllArgsConstructor
    static class App {
        private String name;
        private List<String> serviceInfo;
    }
}
